import { loginAPI, getProfileAPI } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    profile: {}
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    clearUserInfo(state, newToken) {
      state.token = ''
      state.profile = {}
      removeToken()
    },
    setProfile(state, profile) {
      state.profile = profile
    }
  },
  actions: {
    async asyncLogin(ctx, { username, password }) {
      const res = await loginAPI({ username, password })
      ctx.commit('setToken', res.data.token)
    },
    async getProfile(ctx) {
      const res = await getProfileAPI()
      console.log(res, '22222')
      ctx.commit('setProfile', res.data)
      return res.data.permissions
    }

  }
}
